<?php
namespace app\api\controller\supplier;

use app\api\base\RestfulBase;
use app\modules\supplier\call\Auth as SupplierAuthCall;
use Wechat\TPWechat;
use think\Config;
/**
 * Class Login User用户登陆接口控制器
 * @package app\api\controller\user
 */
class Login extends RestfulBase
{
    
    protected $_open_auth = ['user' => false, 'open_api' => false];
    protected  $wechatObj;

    public  function _initialize()
    {
        //微信授权
        $options= Config::get('wechat');
        $this->wechatObj = new TPWechat($options);
    }

    /**
     * 用户登陆接口
     * @return \think\Response
     */
    public function index(){
        if ($this->request->isPost()){
            if($this->_is_login){
                return $this->apiSuccess([],"你已经登录系统，无需重复登录");
            }

            //[ 测试参数 ] 账户登录时是否临时关闭手机验证码
            // $op_sms_code=$this->request->param('op_sms_code','','trim,strip_tags,strtolower');
            // if($op_sms_code==md5(md5(date('Y-m-d',time())))){
            //     config('user_login.enable_sms_code',0);
            // }

            $username=$this->getParams('username','','trim,strip_tags');
            $password=$this->getParams('password','','trim');

            if(true===SupplierAuthCall::loginPassword($username,$password)){
                $this->_account=session('account');
                $this->_is_login=true;

                return $this->apiSuccess(session('account'),"登录成功");
            }else{
                return $this->apiError(SupplierAuthCall::getError(";"),SupplierAuthCall::$_error_code);
            }
        }else{
            return $this->apiError('无效请求');
        }

    }

    /**
     * 微信绑定登录接口
     * @return \think\Response
     */
    public function wx_bind(){
        if ($this->request->isGet()){
            if($this->_is_login){
                return $this->apiSuccess([],"你已经登录系统，无需重复登录");
            }

            $data['username'] = $this->getParams('username','','trim,strip_tags');
            $data['password']=$this->getParams('password','','trim');
            $data['code'] = $this->getParams('code','','trim,strip_tags');

            if(true===SupplierAuthCall::wxBind($data)){
                $this->_account=session('account');
                $this->_is_login=true;

                return $this->apiSuccess(session('account'),"登录成功");
            }else{
                return $this->apiError(SupplierAuthCall::getError(";"),SupplierAuthCall::$_error_code);
            }

        }else{
            return $this->apiError('无效请求');
        }
    }

    /**
     * 微信登录接口
     * @return \think\Response
     */
    public function wx_index(){
        if ($this->request->isGet()){
            if($this->_is_login){
                return $this->apiSuccess([],"你已经登录系统，无需重复登录");
            }

           $code = $this->getParams('code','','trim,strip_tags');

            if(true===SupplierAuthCall::loginWx($code)){
                $this->_account=session('account');
                $this->_is_login=true;

                return $this->apiSuccess(session('account'),"登录成功");
            }else{
                return $this->apiError(SupplierAuthCall::getError(";"),SupplierAuthCall::$_error_code);
            }

        }else{
            return $this->apiError('无效请求');
        }

    }

    public function x_index()
    {
        if ($this->request->isGet()){
            if($this->_is_login){
                return $this->apiSuccess([],"你已经登录系统，无需重复登录");
            }

            $code = $this->getParams('code','','trim,strip_tags');

            if ($code){
                if(true===SupplierAuthCall::loginWx($code)){
                    $this->_account=session('account');
                    $this->_is_login=true;

                    return $this->apiSuccess(session('account'),"登录成功");
                }else{
                    return $this->apiError(SupplierAuthCall::getError(";"),SupplierAuthCall::$_error_code);
                }
            }else{
               return $this->apiSuccess($this->get_oauth());
            }


        }else{
            return $this->apiError('无效请求');
        }
    }


    //获取授权--0:基本信息授权,1:所有信息授权
    protected function get_oauth($all=0)
    {
        $url=$this->get_url();
        $type=$all==1?'snsapi_userinfo':'snsapi_base';
        $wx_url=$this->wechatObj->getOauthRedirect($url,$type,$type);
        return $wx_url;

    }

    //获取当前地址
    public function get_url()
    {
        //协议
        $protocol=isset($_SERVER['SERVER_PORT'])&&$_SERVER['SERVER_PORT']=='443'?'https://':'http://';
        //入口
        $main=$_SERVER['PHP_SELF']?$_SERVER['PHP_SELF']:$_SERVER['SCRIPT_NAME'];
        //参数
        $parm=isset($_SERVER['PATH_INFO'])?$_SERVER['PATH_INFO']:'';
        //地址
        $url=isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:$main.(isset($_SERVER['QUERY_STRING'])?'?'.$_SERVER['QUERY_STRING']:$parm);
        //返回地址
        return $protocol.(isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:'').$url;
    }
}

